package com.qumitech.visualnovel.service.advent.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.advent.AdventAdminQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.advent.AdventAdminSaveDTO;
import com.qumitech.visualnovel.pubdef.dto.advent.AdventAdminUpdateDTO;
import com.qumitech.visualnovel.service.advent.AdventConfigService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: yilu
 * @Date: 2018/11/22 15:08
 * @Description:
 */

@Service
public class AdventConfigServiceImpl implements AdventConfigService {

    private final String ADVENT_CONFIG_URL = "/advent";
    private final String ADVENT_CONFIG_ADMIN_URL = "/admin/advent";

    @Override
    public BaseRsp addAdvent(AdventAdminSaveDTO saveDTO) {
        String url = UrlConstant.SERVER_URL + ADVENT_CONFIG_ADMIN_URL;
        String req = JSONObject.toJSONString(saveDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp updateAdvent(AdventAdminUpdateDTO updateDTO) {
        String url = UrlConstant.SERVER_URL + ADVENT_CONFIG_ADMIN_URL + "/" + updateDTO.getCfgId();
        String req = JSONObject.toJSONString(updateDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp rmAdvent(String cfgId) {
        String url = UrlConstant.SERVER_URL + ADVENT_CONFIG_ADMIN_URL + "/" + cfgId;
        String body = HttpInvokeUtil.httpDelete(url, new HashMap<>());
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        return rsp;
    }

    @Override
    public BaseRsp getAdvents(AdventAdminQueryDTO queryDTO) {
        String url = UrlConstant.SERVER_URL + ADVENT_CONFIG_ADMIN_URL;
        JSONObject o = JSONObject.parseObject(JSONObject.toJSONString(queryDTO));
        Map<String, Object> req = JSONObject.toJavaObject(o, Map.class);
        String body = HttpInvokeUtil.httpGet(url, req);
        return JSONObject.parseObject(body, BaseRsp.class);
    }
}
